package com.github.tomakehurst.wiremock.db.entity;

import com.github.tomakehurst.wiremock.stubbing.StubMapping;

import java.time.LocalDateTime;

/**
 * 纯数据实体类：仅承载 wm_stub_mapping 表字段，无业务逻辑
 */
public class StubMappingEntity {
  // 表字段对应属性
  private String id;
  private Boolean isPersistent;
  private String demandId;
  private String createUserId;
  private boolean permission = true; // 默认true，与表默认值一致
  private String sanitizedUrl;
  private String originalFilename;
  private LocalDateTime createdAt;
  private LocalDateTime updatedAt;
  private String contentId;

  // 所有 Getter/Setter（仅用于字段赋值和取值，无其他逻辑）
  public String getId() { return id; }
  public void setId(String id) { this.id = id; }
  public Boolean getIsPersistent() { return isPersistent; }
  public void setIsPersistent(Boolean isPersistent) { this.isPersistent = isPersistent; }
  public String getDemandId() { return demandId; }
  public void setDemandId(String demandId) { this.demandId = demandId; }
  public String getCreateUserId() { return createUserId; }
  public void setCreateUserId(String createUserId) { this.createUserId = createUserId; }
  public String getSanitizedUrl() { return sanitizedUrl; }
  public void setSanitizedUrl(String sanitizedUrl) { this.sanitizedUrl = sanitizedUrl; }
  public String getOriginalFilename() { return originalFilename; }
  public void setOriginalFilename(String originalFilename) { this.originalFilename = originalFilename; }
  public LocalDateTime getCreatedAt() { return createdAt; }
  public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
  public LocalDateTime getUpdatedAt() { return updatedAt; }
  public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }
  public String getContentId() { return contentId; }
  public void setContentId(String contentId) { this.contentId = contentId; }
  // （关键）将Entity的权限同步到WireMock的StubMapping（自定义扩展字段）
  public void syncPermissionToStubMapping(StubMapping stubMapping) {
    // 若StubMapping无扩展字段，需先定义扩展属性（如用metadata存储权限）
    stubMapping.getMetadata().put("permission", this.permission);
  }

  // （关键）从WireMock的StubMapping反向获取权限（用于新增/更新时赋值）
  public void setPermissionFromStubMapping(StubMapping stubMapping) {
    Object permissionObj = stubMapping.getMetadata().get("permission");
    // 处理空值：默认true（与表默认值一致）
    this.permission = permissionObj == null ? true : (boolean) permissionObj;
  }

  public Boolean getPersistent() {
    return isPersistent;
  }

  public StubMappingEntity setPersistent(Boolean persistent) {
    isPersistent = persistent;
    return this;
  }

  public boolean isPermission() {
    return permission;
  }

  public StubMappingEntity setPermission(boolean permission) {
    this.permission = permission;
    return this;
  }
}